package SchoolProject.Main.Product;
import SchoolProject.JavaBean.OutputMessage;
import SchoolProject.JavaBean.ProductMessage;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class outputMethod {
    public static void menu() throws IOException
    {
        String filePath = "src\\SchoolProject\\file\\OutputMessage.txt";

        ArrayList<OutputMessage> list=new ArrayList<>();
        loop:
        while (true) {
            System.out.println("------------欢迎使用出库管理系统------------");
            System.out.println("1：出库货物");
            System.out.println("2：删除货物");
            System.out.println("3：修改货物");
            System.out.println("4：退出");
            System.out.print("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose) {
                case "1":
                    //添加
                    addProduct(list);
                    break;
                case "2":
                    //删除
                    deletProduct(list);
                    break;
                case "3":
                    //修改
                    changeProduct(list);
                    break;
                case "4":
                    System.out.println("正在退出");
                    moveData();
                    //System.exit(0);//停止虚拟机运行
                    break loop;
                //        ****
                default:
                    System.out.println("0");
                    break;
            }
        }

    }
    private static void moveData() throws IOException
    {

        BufferedReader br = new BufferedReader(new FileReader("src\\SchoolProject\\file\\OutputMessage.txt"));
        String temp ;

        while ((temp = br.readLine()) != null)
        {
            System.out.println(temp);
            String tempID= temp.split(",")[1];

            ProductMessage p =  Product.getProductMessageFromId(tempID);
            if (p==null)
            {
                System.out.println("error:存在未找到的ID!!!");
                System.out.println("这个非法而又不正义的ID为:"+tempID);
                System.out.println("已删除这个非法ID!");
            }else {
                System.out.println("找到"+p.getProductName());
                BufferedReader br2 = new BufferedReader(new FileReader("src\\SchoolProject\\file\\ProductMessage.txt"));
                String  temp2 = br2.readLine();

                String tempID2= temp2.split(",")[0];
                if (tempID2.equals(tempID))
                    System.out.println("找到"+p.getProductName()+"的ID是"+tempID);
                Product.updateProduct(p,Integer.parseInt(temp.split(",")[2]),"delete");


            }
        }
        br.close();

        File file = new File("src\\SchoolProject\\file\\OutputMessage.txt");
        file.delete();
        file.createNewFile();

        System.out.println("数据转移完毕");
    }

    private static void changeProduct(ArrayList<OutputMessage> list) {
        System.out.println("===== 修改货物 =====");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入出库编号");
        String outputID=sc.next();
        int index=checkOutputID(list,outputID);
        if (index==-1){
            System.out.println("未找到");

        }else {
            System.out.println("查找到出库编号为"+outputID+"的货物");
            System.out.println("请选择你要修改的信息");
            System.out.println("1：出库数量");
            System.out.println("2：出库日期");
            System.out.println("3：收货部");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                {
                    changeNum(list,index);
                    saveToFile(list,"src\\SchoolProject\\file\\OutputMessage.txt");
                    break;
                }
                case 2:
                {
                    changeDate(list,index);
                    saveToFile(list,"src\\SchoolProject\\file\\OutputMessage.txt");
                    break;
                }
                case 3:
                {
                    changeSupplier(list,index);
                    saveToFile(list,"src\\SchoolProject\\file\\OutputMessage.txt");
                    break;
                }
                default:
                {
                    System.out.println("选型不存在");
                    break;
                }
            }
        }
    }
    private static void changeNum(ArrayList<OutputMessage> list, int index) {
        OutputMessage temp=list.get(index);
        System.out.println("请输入修改后的数量");
        Scanner sc=new Scanner(System.in);
        temp.setOutputNum(sc.nextInt());
        System.out.println("修改成功");
    }
    private static void changeDate(ArrayList<OutputMessage> list, int index) {
        OutputMessage temp=list.get(index);
        System.out.println("请输入修改后的日期");
        Scanner sc=new Scanner(System.in);
        temp.setOutputDate(sc.next());
        System.out.println("修改成功");
    }
    private static void changeSupplier(ArrayList<OutputMessage> list, int index) {
        OutputMessage temp=list.get(index);
        System.out.println("请输入修改后的供应商");
        Scanner sc=new Scanner(System.in);
        temp.setRecevier(sc.next());
        System.out.println("修改成功");
    }
    private static void deletProduct(ArrayList<OutputMessage> list) {
        System.out.println("===== 删除货物 =====");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入出库编号");
        String outID=sc.next();
        int index=checkOutputID(list,outID);
        if (index==-1){
            System.out.println("未找到");
        }else {
            list.remove(index);
            saveToFile(list,"src\\SchoolProject\\file\\OutputMessage.txt");
            System.out.println("删除成功");
        }
    }
    public static int checkOutputID(ArrayList<OutputMessage> list,String id) {
        for (int i = 0; i < list.size(); i++) {
            OutputMessage temp = list.get(i);
            if (temp.getOutputID().equals(id)) {
                return i;
            }
        }
        return -1;
    }
    private static void addProduct(ArrayList<OutputMessage> list) {
        Scanner sc=new Scanner(System.in);
        OutputMessage ip=new OutputMessage();
        System.out.println("===== 出库货物 =====");
        System.out.println("请输入出库编号");
        ip.setOutputID(sc.next());
        System.out.println("请输入商品编号");
        ip.setProductID(sc.next());
        System.out.println("请输入出库数量");
        ip.setOutputNum(sc.nextInt());
        System.out.println("请输入出库日期(例:YYYY-MM-DD)");
        ip.setOutputDate(sc.next());
        System.out.println("请输入收货部门名称");
        ip.setRecevier(sc.next());
        list.add(ip);
        saveToFile(list,"src\\SchoolProject\\file\\OutputMessage.txt");
        System.out.println("出库成功");
    }
    /**
     * 将集合中的出库信息写入文件（保存）
     */
    public static void saveToFile(ArrayList<OutputMessage> list, String filePath) {
        try (java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(filePath))) {
            for (OutputMessage item : list) {
                // 每行数据格式：出库编号,商品编号,出库数量,出库日期,收货部门
                String line = item.getOutputID() + "," +
                        item.getProductID() + "," +
                        item.getOutputNum() + "," +
                        item.getOutputDate() + "," +
                        item.getRecevier();
                writer.write(line);
                writer.newLine(); // 换行
            }
            System.out.println("出库数据已成功保存到文件：" + filePath);
        } catch (java.io.IOException e) {
            System.err.println("保存出库数据时出错：" + e.getMessage());
        }
    }
    /**
     * 从文件读取出库信息到集合（加载）
     */
    public static void loadFromFile(ArrayList<OutputMessage> list, String filePath) {
        java.io.File file = new java.io.File(filePath);
        if (!file.exists()) {
            System.out.println("出库数据文件不存在，将创建新文件。");
            return; // 文件不存在，不加载，直接返回空集合
        }

        try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                // 按逗号分割每一行
                String[] parts = line.split(",");
                if (parts.length == 5) { // 确保字段数量正确
                    OutputMessage item = new OutputMessage();
                    item.setOutputID(parts[0]);
                    item.setProductID(parts[1]);
                    item.setOutputNum(Integer.parseInt(parts[2]));
                    item.setOutputDate(parts[3]);
                    item.setRecevier(parts[4]);
                    list.add(item);
                } else {
                    System.err.println("文件格式错误，跳过该行：" + line);
                }
            }
            System.out.println("出库数据已从文件加载到内存。");
        } catch (java.io.IOException e) {
            System.err.println("读取出库数据文件时出错：" + e.getMessage());
        } catch (NumberFormatException e) {
            System.err.println("数字解析错误，可能是出库数量格式不对：" + e.getMessage());
        }
    }
}
